'use strict';

angular.module('app')
    .controller('extractApplyController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '应付账款';
            $scope.param = { };
            $scope.loading = false;

            $scope.search = function () {
                $scope.loading = true;
                $scope.param.applyMinDate = $("#applyMinDate").val();
                $scope.param.applyMaxDate = $("#applyMaxDate").val();
                $scope.param.tradeMinDate = $("#tradeMinDate").val();
                $scope.param.tradeMaxDate = $("#tradeMaxDate").val();
                $.ajax({
                    url : $scope.app.project_name + '/extractApply/read/list',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.param.pageNum = 1;
                $scope.param.applyMinDate = $("#applyMinDate").val();
                $scope.param.applyMaxDate = $("#applyMaxDate").val();
                $scope.param.tradeMinDate = $("#tradeMinDate").val();
                $scope.param.tradeMaxDate = $("#tradeMaxDate").val();
                $.ajax({
                    url : $scope.app.project_name + '/extractApply/read/list',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();

            $scope.selected = [];
            var updateSelected = function (action, id) {
                if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
                if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
            };

            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];
                    updateSelected(action, contact.id);
                }
            };

            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };

            $scope.updateSelection = function ($event, id) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(id) == -1) $scope.selected.push(id);
                if (action == 'remove' && $scope.selected.indexOf(id) != -1) $scope.selected.splice($scope.selected.indexOf(id), 1);
            };

            $scope.showApply = function (id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/extractApply/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.extract = result.data;
                        $('#fukuan').modal('show');
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                })
            }
            $scope.Validate = function (form) {
                var flag = true;
                form.find(".require").each(function () {
                    console.log($(this).val());
                    if($(this).val()==""||$(this).val()==null){
                        $(this).tooltip({"trigger":'manual','title':$(this).data('title')+"不能为空"});
                        $(this).tooltip('show');
                        $(this).hover(function () {
                            $(this).tooltip('hide');
                        });
                        flag = false;
                        return false;
                    }

                })
                return flag;
            }

            $scope.saveExtractPay = function () {

                if(!$scope.Validate($("#applyPay"))){
                    return;
                }
                $scope.extract.payDate=$('#payDate').val();
                $scope.isDisabled = true;//提交disabled
                   $.ajax({
                        url: $scope.app.project_name + '/extractApply/update/pay',
                        data: $scope.extract
                    }).then(function (result) {
                        console.log(result);
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            if (result.data > 0) {
                                $('#fukuan').modal('hide');
                                $('#payDate').val("");
                                $scope.alertModal({"content": "付款成功"});
                            } else {
                                $('#fukuan').modal('hide');
                                $('#payDate').val("");
                                $scope.alertModal({"content": "付款失败"});
                            }
                        } else {
                            $('#payDate').val("");
                            $scope.alertModal({"content": "付款失败"});
                        }
                        $scope.isDisabled = false;//提交disabled
                        $scope.search();
                        $scope.$apply();
                    })
                }
            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.search();
            };
            $scope.clearSearch = function() {
                $scope.param= {};
                $("#applyMinDate").val("");
               $("#applyMaxDate").val("");
               $("#tradeMinDate").val("");
               $("#tradeMaxDate").val("");
                $scope.search();
            }
        } ]);